#include <bits/stdc++.h>
using namespace std;
int a[100001],c[100001],aa[100001];
string str[31];
int n,m;
int dfs(int x,int y,int z,int k)
{
	if(x+1==y)
	{
		if(str[int(log2(y-x+1)+log2(n*2-1)-log2(k))][y/2-1]=='0')
		{
			if(aa[x]>=z)
				return x;
			else
				return y;
		}
		else 
		{
			if(aa[y]>=z)
				return x;
			else 
				return y;
		}
	}
	int a=dfs(x,(x+y)/2,z-1,k),b=dfs((x+y)/2+1,y,z-1,k);
	if(str[int(log2(y-x+1)+log2(n*2-1)-log2(k))][y/2-1]=='0')
	{
		if(aa[x]>=z)
			return a;
		else 
			return b;
	}
	else
	{
		if(aa[y]>=z)
			return a;
		else
			return b;
	}
}
int main()
{
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)
		scanf("%d",&c[i]);
	for(int i=1;i<=int(log2(n*2-1));i++)
		cin>>str[i];
	int t;
	cin>>t;
	while(t--)
	{
		int	x[4],ans=0;
		cin>>x[0]>>x[1]>>x[2]>>x[3];
		for(int i=1;i<=n;i++)
			aa[i]=a[i]^x[i%4];
		for(int i=1;i<=m;i++)
			ans^=dfs(1,c[i],int(log2(c[i]-1+1)),c[i]);
		cout<<ans<<endl;
	}
	return 0;
}
